#include<bits/stdc++.h>
#define ll long long
#define inf 1000000007
using namespace std;
ll T,n,a[500005],dp[500005];
bool f[10005];
int f1[505],f2[505],cnt1,cnt2,ansz;
void dfs(int u)
{
    if(u == n + 1)
    {
        cnt1 = 0,cnt2 = 0;
        for(int i = 1;i <= n;i++)
            if(f[i] == 1)
                f1[++cnt1] = a[i];
            else 
                f2[++cnt2] = a[i];
        int ans = 0;
        for(int i = 2;i <= cnt1;i++)
            if(f1[i] == f1[i - 1])
                ans += f1[i];
        for(int i = 2;i <= cnt2;i++)
            if(f2[i] == f2[i - 1])
                ans += f2[i];
        ansz = max(ansz,ans);

        return;
    }
    f[u] = 1;
    dfs(u + 1);
    f[u] = 0;
    dfs(u + 1);
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> T;
    while(T--)
    {
        cin >> n ;
        for(int i = 1;i <= n;i++)
            cin >> a[i];
        if(n <= 15)
        {
            ansz = 0;
            dfs(1);
            cout << ansz << endl ;
        }
        else
        {
            for(int i = 1;i <= n;i++)
            {
                for(int j = 1;j < i;j++)
                    if(a[j] == a[i])
                    {
                        ll sum = 0;
                        for(int k = j;k < i;k++)
                            sum += dp[k];   
                        dp[i] = max(sum + a[i],dp[i]);
                    }
            }
            ll ans = 0;
            for(int i = 1;i <= n;i++)
                ans = max(ans,dp[i]);
            cout << ans << endl ;
            memset(dp,0,sizeof dp);
        }
    }
    return 0;
}//Seeing freinds surrunding me going away.